// index.js
// 测试图片
const url = 'http://cloudcdn.tch1217.com/Fm7_gXyenIkDuJAyenmc8wju8WaQ?imageView2/1/w/50/h/50/q/100'
// 获取应用实例
const app = getApp()
//引入接口
import {
	getopenid,
	getimagelist,
	getlinelist,
	imagedelete
} from '../../api/cloud.js';
// 触摸开始时间
let touchStartTime = 0
// 触摸结束时间
let touchEndTime = 0
// 最后一次单击事件点击发生时间
let lastTapTime = 0
// 单击事件点击后要触发的函数
let lastTapTimeoutFunc = null
Page({
	data: {
		//显示弹窗
		iosDialog1: false,
		// 图像集合
		imageList: [],
		// 最后一个时间
		YTdtime: null,
		//是否有图像
		condition: false,
		//是否可以下拉
		down: true,
		//标题加载状态
		nbLoading: false,
		//选中数量
		num: 0,
		//选中的图片id集合
		checkarr: [],
		// 是否显示多选框
		is_check: false,
		hideLoading: false,
		loading: false,
		//当前多选选择的图片
		imageUrl:null

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad() {
		this.getlist();
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
		// console.log('onReady')
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		// console.log('onShow')
	},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {
		this.setData({
			iosDialog1: false,
			imageList: [],
			YTdtime: null,
			condition: false,
			down: true,
			nbLoading: false,
			num: 0,
			checkarr: [],
			is_check: false
		});
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {
		// console.log('onHide')
	},
	/**
	 * 按钮触摸开始触发的事件
	 */
	touchStart(e) {
		this.touchStartTime = e.timeStamp
	},
	/**
	 * 按钮触摸结束触发的事件
	 */
	touchEnd(e) {
		this.touchEndTime = e.timeStamp
	},
	/**
	 * 判断单点事件和长按
	 */
	multipleTap(e) {
		var that = this
		// 控制点击事件在350ms内触发，加这层判断是为了防止长按时会触发点击事件
		if (that.touchEndTime - that.touchStartTime < 350) {
			// 当前点击的时间
			this.bigimg(e);
		} else {
			this.startCheck(e);
		}
	},

	/**
	 * 获取相册列表
	 */
	getlist() {
		this.setData({
			nbLoading: true
		})
		getlinelist({
			YTdtime: this.data.YTdtime,
		}).then(rs => {
			let length = rs.data.length
			if (length > 0) {
				this.setData({
					imageList: [...this.data.imageList, ...rs.data],
					condition: true,
					nbLoading: false
				})
				this.getYTdtime()
			}
			if (length < 6) {
				this.setData({
					down: false,
					nbLoading: false
				})
			}
		})
	},
	/**
	 * 请求时间轴重新取值获取
	 */
	getYTdtime() {
		let arr = this.data.imageList.slice(-1)
		this.data.YTdtime = arr[0]['YTdtime']
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {
		if (this.data.down) {
			this.getlist()
		} else {
			wx.showToast({
				title: '没有数据了',
				icon: 'error'
			})
		}
	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {
		wx.stopPullDownRefresh()
		this.setData({
			iosDialog1: false,
			imageList: [],
			YTdtime: null,
			condition: false,
			down: true,
			nbLoading: false,
			num: 0,
			checkarr: [],
			is_check: false
		});
		this.getlist()

	},
	/**
	 * 放大图片查看
	 */
	bigimg(e) {
		let data = this.data.imageList
		let item = e.target.dataset.item
		let items = e.target.dataset.items
		let imgurl = data[item]['list'][items]['urlbig']
		let list = data[item]['list']
		let arr = []
		let listurl = list.filter(index => {
			arr.push(index.urlbig)
			return index.url
		})
		wx.previewImage({
			current: imgurl, // 当前显示图片的 http 链接
			urls: arr // 需要预览的图片 http 链接列表
		})
	},
	/**
	 * 触发多选
	 */
	startCheck(e) {
		let data = this.data.imageList
		let item = e.target.dataset.item
		let items = e.target.dataset.items
		// console.log(data)
		// console.log(item)
		// console.log(items)
		let checked = data[item]['list'][items]['checked']
		let id =data[item]['list'][items]['id']
		let imageUrl = data[item]['list'][items]['urlbig']
		let checkarr =[]
		checkarr.push(id)
		data[item]['list'][items]['checked'] = !checked
		this.setData({
			imageList: data,
			is_check: true,
			num: 1,
			checkarr:checkarr,
			imageUrl:imageUrl
		})
	},
	/**
	 * 多选回调触发
	 */
	checkboxChange(e) {
		let is_check = e.detail.value.length == 0 ? false : true
		this.setData({
			checkarr: e.detail.value,
			num: e.detail.value.length,
			is_check: is_check
		})

	},
	/**
	 * 全选
	 */
	allCheck() {
		let id_arr = []
		let data = this.data.imageList
		let list = data.filter(index => {
			index.list.filter(item => {
				id_arr.push(item.id.toString())
				item.checked = true
				return item
			})
			return index
		})

		this.setData({
			checkarr: id_arr,
			imageList: list,
			num: id_arr.length
		})
	},
	/**
	 * 取消
	 */
	cancel() {
		let id_arr = []
		let data = this.data.imageList
		let list = data.filter(index => {
			index.list.filter(item => {
				id_arr.push(item.id.toString())
				item.checked = false
				return item
			})
			return index
		})
		this.setData({
			checkarr: id_arr,
			imageList: list,
			is_check: false,
			num: 0,
			checkarr: []
		})
	},
	/**
	 * 关闭弹窗
	 */
	close() {
		this.setData({
			iosDialog1: false,
		});
	},
	/**
	 * 删除弹窗
	 */
	openIOS1() {
		this.setData({
			iosDialog1: true,
		});
	},
	/**
	 * 删除
	 */
	delete() {
		this.setData({
			hideLoading: true,
			loading: true
		})
		let data = this.data.imageList
		let ids = this.data.checkarr
		data.forEach((item, index) => {
			data[index]['list'] = item.list.filter(items => {
				return (items.checked != true)
			})
		})
		data.forEach((item, index) => {
			data[index]['list'] = item.list.filter(items => {
				if(items.checked == true)
				{
					ids.push(items.id)
				}
				return items
			})
		})
		let ids_str =ids.toString()
		//请求后台
		imagedelete({
			ids: ids_str
		}).then(res => {
			wx.showToast({
				title: '删除成功',
				icon: 'success'
			})
			this.setData({
				imageList: data,
				num: 0,
				is_check: false,
				iosDialog1: false,
				hideLoading: false,
				loading: false
			})
		}).catch(err => {
			wx.showToast({
				title: err.msg,
				icon: 'error'
			})
			this.setData({
				imageList: data,
				num: 0,
				is_check: false,
				iosDialog1: false,
				hideLoading: false,
				loading: false
			})
		})
		

	},
	/**
	 * 多选模式点击图片触发
	 */
	tap(e) {
		let data = this.data.imageList
		let item = e.target.dataset.item
		let items = e.target.dataset.items
		let checked = data[item]['list'][items]['checked']
		data[item]['list'][items]['checked'] = !checked
		this.setData({
			imageList: data,
		})
	},
	/**
	 * 触发转发
	 */
	buttonShare(){
		console.log('1')
		// wx.updateShareMenu({
		// 	withShareTicket: true,
		// 	success () { 
		// 		console.log('2')
		// 	}
		//   })
		wx.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage', 'shareTimeline']
		  })
	},
	/**
	 * 分享
	 */
	 onShareAppMessage(){
		let that =this
		let ids =that.data.checkarr
		let imageUrl =that.data.imageUrl
		let ids_str =ids.toString()
		let path = 'pages/share/share?ids='+ids_str
		return{
			title:'好友分享',
			path:path,
			imageUrl:imageUrl

		}
	 },
	 onShareTimeline(){}

})
